/**
 * author: ares
 * date: 2020/3/7 19:56
 * title: ctx-success-func
 */


/**
 * @author: ares
 * @date: 2020/3/7 20:08
 * @description: 重写ctx success,定义success方法的默认code和msg
 * @param {object}ctx
 */
function overwriteSuccessFunc(ctx){
	ctx.success = function(data = '', message = 'success') {
		const obj = {
			[this.config('errnoField')]: this.config('defaultSuccessno') || 2000,
			[this.config('errmsgField')]: message,
			data
		};
		this.type = this.config('jsonContentType');
		this.body = obj;
		return false;
	}
}

module.exports=function(){
	return async function(ctx,next){
		overwriteSuccessFunc(ctx);
		await next();
	}
}